package com.gla.market.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gla.market.R;
import com.gla.market.entity.FYH_Evaluation;
import com.gla.market.entity.FYH_Shop;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

public class FYH_EvaluationAdapter extends BaseAdapter {
    private Context context;
    private List<FYH_Evaluation> listEvaluation;

    public FYH_EvaluationAdapter(Context context, List<FYH_Evaluation> listEvaluation) {
        this.context = context;
        this.listEvaluation = listEvaluation;
        //实例化ImageLoader
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(context);
        ImageLoader.getInstance().init(configuration);
    }

    @Override
    public int getCount() {
        return listEvaluation.size();
    }

    @Override
    public Object getItem(int i) {
        return listEvaluation.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        FYH_EvaluationHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.fyh_evaluation_card, null);

            //布局文件中所有组件的对象封装到ViewHolder对象中
            holder = new FYH_EvaluationHolder();
            holder.eusername = convertView.findViewById(R.id.e_user_name);
            holder.epoint = convertView.findViewById(R.id.e_point);
            holder.euserphoto = convertView.findViewById(R.id.e_user_photo);
            holder.evalu = convertView.findViewById(R.id.evaluation);
            //把ViewHolder对象封装到View对象中
            convertView.setTag(holder);
        } else { holder = (FYH_EvaluationHolder) convertView.getTag(); }

        //获取点击的子菜单的View
        FYH_Evaluation ev = listEvaluation.get(position);
        String name = ev.getUser_name();
        if(name.length() > 6) name =  name.substring(0,6) + "...";
        String photo = ev.getUser_photo();
        String eva = ev.getEvaluation();
        int point = ev.getPoint();

        holder.eusername.setText(name);
        holder.epoint.setText(point + "分");
        holder.evalu.setText(eva);
        //绑定图片到控件
        if(!"".equals(photo)) {
            ImageLoader.getInstance().displayImage(photo, holder.euserphoto);
        }else{
            holder.euserphoto.setImageResource(R.drawable.hhn_user);
        }
        return convertView;
    }
}
